<template>
	<view class="cont" v-if="isLogin">
		<navi-shell :back="true" v-bind:title="users.length + ' 位用户'">
			<uni-search-bar placeholder="请输入搜索关键字" class="uni-searchbar" :redius="100" cancelButton="none"
				@confirm="search" @clear="clearKeywords"></uni-search-bar>
		</navi-shell>
		<view class="content">
			<view class="sort" @click="csort(0)">
				<text>所有</text>
			</view>
			<view class="sort" @click="csort(1)">
				<text>申请教师</text>
			</view>
			<view class="sort" @click="csort(2)">
				<text>教师</text>
			</view>
			<view class="sort" @click="csort(3)">
				<text>学生</text>
			</view>
			<view class="sort" @click="csort(4)">
				<text>已禁止</text>
			</view>
		</view>
		<view class="user-dis">
			<view v-for="(user,index) in users" :key="user.id" class="user-display">
				<image :src="user.avatar" mode="aspectFill" class="img-avatar" @click.stop="clickShowUser(user)">
				</image>
				<text class="iconfont icon-B-xialaliebiao ic-b-x" @click.stop="clickPopMenu(user)"></text>
				<text v-if="user.applyTeacher==true" class="user-applying">申请中</text>
				<text class="user-text">{{user.nickName+"[" + userTypes[user.type +1] + "]"}}</text>
			</view>
		</view>
		<login-dialog ref="login" @after="afterLogin"></login-dialog>
		<dialog-shell ref="shell" title="个人信息">
			<text class="user-news">呢称：{{userDescNick}}</text>
			<text class="user-news">用户名：{{userDescName}}</text>
			<text class="user-news">身份：{{userDescType}}</text>
		</dialog-shell>
	</view>
</template>

<script>
	const app = getApp()
	export default {
		data() {
			return {
				isLogin: false,
				searching: false,
				users: [],
				userDescNick: "",
				userDescType: "",
				userDescName: "",
				nuber: '',
				uid: '',
				userTypes: ["禁止访问", "管理员", "教师", "学生"]
			}
		},
		onLoad() {
			this.csort(1)
		},
		onPullDownRefresh() {
			this.users = [],
				this.csort(1)
			console.log("执行了下拉刷新")
			uni.stopPullDownRefresh()
		},
		mounted() {
			this.isLogin = app.globalData.token.length > 0
			if (!this.isLogin) {
				delta = 2
				this.$refs.login.show()
			}
		},
		methods: {
			afterLogin() {
				this.isLogin = app.globalData.token.length > 0
				if (this.isLogin) {
					this.csort()
				}
			},
			csort(nuber) {
				let url
				switch (nuber) {
					case 0:
						url = this.$params.host + this.$params.user_root + this.$params.user_all
						break
					case 1:
						url = this.$params.host + this.$params.user_root + this.$params.user_applying
						break
					case 2:
						url = this.$params.host + this.$params.user_root + this.$params.user_teacher
						break
					case 3:
						url = this.$params.host + this.$params.user_root + this.$params.user_student
						break
					case 4:
						url = this.$params.host + this.$params.user_root + this.$params.user_baned
						break
				}
				let data = {
					token: app.globalData.token
				}
				this.$request.getParams(url, data, res => {
					console.log(res)
					res.data.forEach(b => {
						if (!b.avatar.startsWith('http')) {
							b.avatar = this.$params.host + b.avatar
						}
					})
					console.log(nuber)
					this.users = res.data
				});
			},
			clickShowUser(user) {
				this.userDescNick = user.nickName
				this.userDescName = user.userName
				this.userDescType = this.userTypes[user.type + 1]
				this.$refs.shell.showDialog()
			},
			clearKeywords() {
				this.searching = false
				this.users = []
				this.csort(1)
			},
			search(e) {
				let kw = e.value
				if (kw.trim().length == 0) {
					return
				}
				this.searching = true
				this.users = []
				let data = {
					token: app.globalData.token
				}
				let url = this.$params.host + this.$params.user_root + this.$params.user_search + kw
				this.$request.getParams(url, data, res => {
					res.data.forEach(b => {
						if (!b.avatar.startsWith('http')) {
							b.avatar = this.$params.host + b.avatar
						}
					})
					this.users = res.data
					console.log(res)
				})
			},
			clickPopMenu(user) {
				uni.showActionSheet({
					itemList: ['禁止登录', '审批通过', '驳回申请'],
					success: res => {
						if (res.tapIndex == 0) {
							this.userBan(user.id);
						} else if (res.tapIndex == 1) {
							this.userApprove(user.id)
						} else {
							this.userDecline(user.id)
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			userBan(id) {
				let data = {
					token: app.globalData.token,
					"id": id
				}
				let url = this.$params.host + this.$params.user_root + this.$params.user_ban
				this.$request.postParams(url, data, res => {
					console.log(url)
					this.users.forEach(b => {
						if (b.id == id) {
							b.type = res.data.type
						}
					})
				})
			},
			userApprove(id) {
				let data = {
					token: app.globalData.token,
					"id": id
				}
				let url = this.$params.host + this.$params.user_root + this.$params.user_approve
				this.$request.postParams(url, data, res => {
					this.users.forEach(b => {
						if (b.id == id) {
							b.apply = res.data.applyTeacher
							b.type = res.data.type
						}
					})
				})
			},
			userDecline(id) {
				let data = {
					token: app.globalData.token,
					"id": id
				}
				let url = this.$params.host + this.$params.user_root + this.$params.user_decline
				this.$request.postParams(url, data, res => {
					this.users.forEach(b => {
						if (b.id == id) {
							b.apply = res.data.applyTeacher
						}
					})
				})
			}
		}
	}
</script>

<style>
	@import url("usermanage.css");
</style>
